package com.common.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtil {
	/**
	 * 判断是否为空，空串或null为true
	 * @param value
	 * @return 
	 */
	
	public static String uniqueKey(){
		  String key = UUID.randomUUID().toString();
		  key = key.replace("-", "");
		  return key.toUpperCase();
	  } 
	
	public static boolean isEmpty(String value){
		return null==value || "".equals(value);
	}
	public static boolean isBlank(String value){
		return null==value || "".equals(value.trim());
	}
	/**
	 * 将字符串统一转UTF-8
	 * @param content
	 * @return
	 */
	public static String isoToUTF8(String content){
		try {
			if("GB2312".equals(getEncoding(content)))
			{
				return content;
			}
			return new String(content.getBytes(getEncoding(content)),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	/**
	 * 
	 * @param str
	 * @return
	 */
    public static String getEncoding(String str) {      
        String encode = "GB2312";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s = encode;      
               return s;      
            }      
        } catch (Exception exception) {      
        }      
        encode = "ISO-8859-1";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s1 = encode;      
               return s1;      
            }      
        } catch (Exception exception1) {      
        }      
        encode = "UTF-8";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s2 = encode;      
               return s2;      
            }      
        } catch (Exception exception2) {      
        }      
        encode = "GBK";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s3 = encode;      
               return s3;      
            }      
        } catch (Exception exception3) {      
        }      
       return "";      
    }
    /**
     * 获取UUID
     * @return
     */
 	public static String getUUID(){
		String uuid[] = UUID.randomUUID().toString().split("-");
		StringBuffer id = new StringBuffer();
		for (String string : uuid) {
			id.append(string);
		}
		return id.toString();
	}
 	
 	
 	
 	/**
 	 * 将前端的datebox时间格式为06/30/2016 09:19 转为2016-06-30 09:19
 	 * @return
 	 */
 	public static String changeDateBoxTime(String boxtime){
 		String[] times = boxtime.split(" ");
 		String[] dates =  times[0].split("/");
 		StringBuffer sb = new StringBuffer();
 		
 		return sb.append(dates[2]+"-"+dates[0]+"-"+dates[1]+" "+times[1]).toString();
 	}
 	
 	/**
 	 * 
 	 * @author leixin
 	 * @createTime 上午9:38:17
 	 * @param strings  带转换字符串数组
 	 * @param str 需要在数组中间加入的间隔字符串
 	 * @return
 	 */
 	public static String join(String[] strings, String str) {
 		StringBuilder result = new StringBuilder();
 		if (strings.length == 0 || null == strings) {
 			return null;
		}else {
			for (int i = 0; i < strings.length - 1; i++) {
				result = result.append(strings[i]+str);
			}
			result = result.append(strings[strings.length - 1]);
		}
		return result.toString();
	}
 	
 	/**
 	 * 取随机的大写字母，拼接成字符串
 	 * @author leixin
 	 * @createTime 下午4:11:20
 	 * @param num 需要生产的随机字母的个数
 	 * @return
 	 */
 	public static String randomAlphabet(int num) {
 		char[] alphabet = new char[num];
 		for (int i = 0; i < num; i++) {
 			int randomNumber = 65 + (int) (Math.random()* 26);
 			alphabet[i] = (char)randomNumber;
		}
 		return String.valueOf(alphabet);
		
	}
 	
 	/**
 	 * 获取请求URI的方法
 	 * 
 	 * @param uri
 	 * @return
 	 */
 	public static String getMethodURI(String uri){
 		String[] methods = uri.split("/");
 		return methods[methods.length-1];
 	}

	public static String decodeToUTF8(String str){
		if(str!=null&&!str.equals("")){
			try {
				return new String(str.getBytes("ISO-8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return "";
			}
		}
		return null;
	}
	
	/*
	 * 截取字符串中所有括号内的内容，结果返回list
	 */
	public static List<String> getSubString(String a){
		List<String> list=new ArrayList<String>();  
        Pattern p = Pattern.compile("((?<=\\（)|(?<=\\())(.+?)((?=\\）)|(?=\\)))");  
        Matcher m = p.matcher(a);
        while(m.find()){
            list.add(m.group().substring(0, m.group().length()));  
        }  
        return list;
	}
}
